﻿using ProjectSemIII.Administrator.Process;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSemIII.Administrator.Account
{
    public partial class PendingAccount : System.Web.UI.Page
    {
        private List<Entity.User> listUser;
        private const bool blnAllowPaging = true;
        private const int iPageSize = 5;
        private static PagedDataSource pgdProducts;
        private AccountProcess process;
        protected void Page_Load(object sender, EventArgs e)
        {
            pgdProducts = new PagedDataSource();
            process = new AccountProcess();
            if (!Page.IsPostBack)
            {
                this.getAllUserPending();
            }
        }

        protected void btPrev_Click(object sender, EventArgs e)
        {
            LinkButton lbuton = (LinkButton)sender;
            if (lbuton.ID == "btPrev")
            {
                if (ThisPageNumber > 0)
                {
                    ThisPageNumber = ThisPageNumber - 1;
                    this.getAllUserPending();
                }
            }
            //Change page when user click to next number button
            else if (lbuton.ID == "btNext")
            {
                if (ThisPageNumber < totalPage.Items.Count - 1)
                {
                    ThisPageNumber = ThisPageNumber + 1;
                    this.getAllUserPending();
                }
            }
        }

        protected void PageNumber_Click(object sender, EventArgs e)
        {
            LinkButton lbuton = (LinkButton)sender;
            ThisPageNumber = Convert.ToInt32(lbuton.Text) - 1;
            this.getAllUserPending();
        }

        protected void allUserPending_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int idUser = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "Active")
            {
                int result = process.updateStatusUser(idUser, 1);
                if (result == 10) {
                    lbError.Text = "Actived this account succesful!";
                    this.getAllUserPending();
                }
                else
                {
                    lbError.ForeColor = System.Drawing.Color.Red;
                    lbError.Text = "Error in active process this account!";
                }
            }
            else if (e.CommandName == "Decline")
            {
                int result = process.updateStatusUser(idUser, 2);
                if (result == 10)
                {
                    lbError.Text = "Declined this account succesful!";
                    this.getAllUserPending();
                }
                else
                {
                    lbError.ForeColor = System.Drawing.Color.Red;
                    lbError.Text = "Error in decline process this account!";
                }
            }
        }

        private void getAllUserPending()
        {
            listUser = process.getAllAccountByStatus(0);

            pgdProducts.AllowPaging = blnAllowPaging;
            pgdProducts.PageSize = iPageSize;
            pgdProducts.DataSource = listUser;

            pgdProducts.CurrentPageIndex = ThisPageNumber;
            fillTotalPage(pgdProducts.PageCount);

            allUserPending.DataSource = pgdProducts;
            allUserPending.DataBind();
        }

        private void fillTotalPage(int iTotalPages)
        {
            List<int> pageID = new List<int>();
            for (int i = 1; i <= iTotalPages; i++)
            {
                pageID.Add(i);
            }
            totalPage.DataSource = pageID;
            totalPage.DataBind();
        }

        private int ThisPageNumber
        {
            get
            {
                object obj = ViewState["ThisPageNumber"];
                return (obj == null) ? 0 : (int)obj;
            }
            set
            {
                this.ViewState["ThisPageNumber"] = value;
            }
        }
    }
}